//
//  HOTMemoRangeViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-22.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTCommChooseViewController.h"
#import "HOTCommChooseCell.h"
#import "ZZZUIScrollView+Extends.h"


@interface HOTCommChooseViewController () <UITextFieldDelegate>{
    
    NSDictionary *_choosedInfo;
    NSIndexPath *_chooseIndexPath;
}

@end

@implementation HOTCommChooseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil choosedInfo:(NSDictionary *)chooseInfo
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _choosedInfo = chooseInfo;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self setNaviTitle:@"血液型"];
    
    //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_profile"]];
    
    [self addRightBtnTarget:self action:@selector(descideBtnClicked) image:[UIImage imageNamed:@"btn_decision_off"] hImage:[UIImage imageNamed:@"btn_decision_on"]];
    
    //_dataList = @[@{@"id":@"1", @"name":@"A型"},@{@"id":@"2", @"name":@"B型"},
    //              @{@"id":@"3", @"name":@"O型"},@{@"id":@"4", @"name":@"AB型"}];
    
    if (self.isJobType) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidde:) name:UIKeyboardWillHideNotification object:nil];
    }
}

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = [_dataList objectAtIndex:indexPath.row];
    NSString *id_ = [dic objectForKey:@"id"];
    NSString *name = [dic objectForKey:@"name"];
    
    HOTCommChooseCell *cell;
    
    if (self.isJobType && ([id_ isEqualToString:@"0"] || [name isEqualToString:@"その他"])) {
        //当前是职种的选择时
  
        static NSString *cellId = @"custCellID";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        
        if (cell == nil) {
            cell = self.jobCustCell;
        }
        
        if (_chooseIndexPath) {
            if (_chooseIndexPath.row == indexPath.row) {
                cell.radioBtn.selected = YES;
            } else {
                cell.radioBtn.selected = NO;
            }
            
        } else {
            
            if ([[_choosedInfo objectForKey:@"name"] isEqualToString:@"その他"]) {
                _chooseIndexPath = indexPath;
                cell.radioBtn.selected = YES;
            } else {
                cell.radioBtn.selected = NO;
            }
        }
    
        return cell;
        
    }
    
    static NSString *cellId = @"cellID";
    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTCommChooseCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    

    cell.cateTitleLabel.text = name;
    
    
    if (_chooseIndexPath) {
        if (_chooseIndexPath.row == indexPath.row) {
            cell.radioBtn.selected = YES;
        } else {
            cell.radioBtn.selected = NO;
        }
        
    } else {
        
        if ([[_choosedInfo objectForKey:@"id"] isEqualToString:id_]) {
            _chooseIndexPath = indexPath;
            cell.radioBtn.selected = YES;
        } else {
            cell.radioBtn.selected = NO;
        }
    }
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 62;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HOTCommChooseCell *cell;
    if (_chooseIndexPath) {
        
        HOTCommChooseCell *cell = (HOTCommChooseCell *)[tableView cellForRowAtIndexPath:_chooseIndexPath];
        cell.radioBtn.selected = NO;
    }
    
    _chooseIndexPath = indexPath;
    cell = (HOTCommChooseCell *)[tableView cellForRowAtIndexPath:_chooseIndexPath];
    cell.radioBtn.selected = YES;
}

#pragma mark -  UITextField delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (self.isJobType) {
        
        [_tableView.delegate tableView:_tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:_dataList.count-1 inSection:0]];
    }
    
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [textField nextResponder] ;
}

-(void)keyboardWillShow:(NSNotification *)notification {
    
    NSDictionary *userInfo = notification.userInfo;
    
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardFrame;
    
    [value getValue:&keyboardFrame];
    
    CGRect kFrameInView = [self.view convertRect:keyboardFrame fromView:[[UIApplication sharedApplication] keyWindow]];
    
//    [_tableView setContentSize:CGSizeMake(_tableView.contentSize.width, _tableView.contentSize.height + kFrameInView.size.height)];
//    
//    [_tableView setContentOffset:CGPointMake(0, self.view.frame.size.height - kFrameInView.size.height) animated:YES];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        _tableView.frame = CGRectMake(0, -kFrameInView.size.height, _tableView.frame.size.width, _tableView.frame.size.height);
    }];
    
    //[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_dataList.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
}

-(void)keyboardWillHidde:(NSNotification *)notification {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        _tableView.frame = CGRectMake(0, 0, _tableView.frame.size.width, _tableView.frame.size.height);
    }];
}


#pragma mark 私有函数
-(void)descideBtnClicked {
    
    [_otherJobText resignFirstResponder];
    
    
    NSDictionary *dic = nil;
    
    if (self.isJobType) {
        
        if (_chooseIndexPath == nil) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:@"業種を選択してください。"
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
            
            return;
        }
        
        dic = [self.dataList objectAtIndex:_chooseIndexPath.row];
        
        NSString *sId = [dic objectForKey:@"id"];
        
        if ([sId isEqualToString:@"0"] /*判断当前选择的是“その他”*/) {
        
            
            if ([HOTUtils isNullOrEmpty:_otherJobText.text]) { /*その他時、自定义职种名没有输入 */
                //当前为职位选择并且自定义职种未填写时有以下提示
                
                ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                      message:@"業種（その他）を入力してください。"
                                                                     delegate:nil
                                                            cancelButtonTitle:nil
                                                             otherButtonTitle:ALERT_BTN_OK];
                
                [popupView showInWindow];
                
                return;
            }
            
            //将name中的その他替换为自定义输入的职种
            dic = @{@"id":sId, @"name":[HOTUtils getString:_otherJobText.text]};
        }
    }
    
    if (self.choosedDataBlock) {
        
        
        self.choosedDataBlock(dic);
    }

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
